///
/// @ProjectName:    learning_rust
/// @Description:    
/// @File:           jz22.rs
/// @Author:         wujehy
/// @Email:          wujehy@qq.com
/// @Time:           2021-07-03 22:59
///
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct TreeNode {
    pub val: i32,
    pub left: Option<Box<TreeNode>>,
    pub right: Option<Box<TreeNode>>,
}

impl TreeNode {
    #[inline]
    fn new(val: i32) -> Self {
        TreeNode {
            val: val,
            left: None,
            right: None,
        }
    }
}


struct Solution {}

impl Solution {
    fn new() -> Self {
        Solution {}
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param root TreeNode类
     * @return int整型一维数组
     */
    pub fn PrintFromTopToBottom(&self, root: Option<Box<TreeNode>>) -> Vec<i32> {
        // write code here

        // bfs
        // 缓存同一层的节点
        let mut temp_stack = vec![root];
        // 需要返回的列表
        let mut result_vec = vec![];

        while let Some(node_opt) = temp_stack.pop() {
            node_opt.map(|node| {
                result_vec.push(node.val);
                temp_stack.insert(0 , node.left);
                temp_stack.insert(0 , node.right);
            });
        };
        result_vec

    }
}

#[test]
fn test22() {
    let s = Solution::new();


    let l2_1 = Some(Box::new(TreeNode::new(21)));
    let l2_2 = Some(Box::new(TreeNode::new(22)));
    let l2_3 = Some(Box::new(TreeNode::new(23)));
    let l2_4 = Some(Box::new(TreeNode::new(24)));
    let l1_1 = Some(Box::new(TreeNode {
        val: 11,
        left: l2_1,
        right: l2_2
    }));

    let l1_2 = Some(Box::new(TreeNode {
        val: 12,
        left: l2_3,
        right: l2_4,
    }));

    let mut root = Some(Box::new(TreeNode {
        val: 1,
        left: l1_1,
        right: l1_2,
    }));


    let expext = vec![1, 11, 12, 21, 22, 23, 24];

    let  result = s.PrintFromTopToBottom(root);
    assert_eq!(expext , result ) ;
}
